﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RN.ERP.Web.EnterpriseService;
using RN.ERP.Web.BasicService;
using RN.ERP.Web.ModelService;
using Newtonsoft.Json;
using Fly.Common.Linq;
using Fly.Common.WorkFlow;

namespace RN.ERP.Web.Enterprise
{
    /// <summary>
    /// c++调用，没有用户信息，只查看
    /// 2014-2-17 
    /// 毛凯
    /// </summary>
    public partial class EnterpriseQualityInfoForCpp : System.Web.UI.Page
    {
        private EnterpriseServiceClient enterpriseServiceClient = new EnterpriseServiceClient();
        private BasicServiceClient basicServiceClient = new BasicServiceClient();
        /// <summary>
        /// 资质guid
        /// </summary>
        private string QualityGuid
        {
            get
            {
                return Request.QueryString["QualityGuid"];
            }
        }
        private string CorpUserGuid
        {
            get
            {
                return Request.QueryString["CorpUserGuid"];
            }
        }
        /// <summary>
        /// 操作类型
        /// Add：添加,Edit:修改
        /// </summary>
        private string OperateType
        {
            get
            {
                return Request.QueryString["OperateType"];
            }
        }
        private string AreaCode
        {
            get
            {
                return Request["areaCode"] == null ? "" : Request["areaCode"];
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //Guid guid = Guid.Empty;
                //string corpUserGuid = GetCorpUserGuid();
                //if (string.IsNullOrEmpty(corpUserGuid))
                //{
                //    return;
                //}
                //List<QueryCompanyInfoCondition> list = new List<QueryCompanyInfoCondition>();
                //QueryCompanyInfoCondition condition = new QueryCompanyInfoCondition();
                //condition.CorpUserGuid = new ParameterValue<Guid>() { O = OptEnum.Equal, V = Guid.Parse(corpUserGuid) };
                //list.Add(condition);
                //var corp = JsonConvert.DeserializeObject<CompanyInfo>(enterpriseServiceClient.GetCompanyBisicInfo(JsonConvert.SerializeObject(list), AreaCode));
                //if (corp == null)
                //{
                //    ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('地区错误或企业未开通该市场范围')", true);
                //    //ScriptManager.RegisterStartupScript(this, this.GetType(), "", "window.opener=null;window.open('','_self');window.close();", true);
                //    //return;
                //}
                BindData();
                InitPage();
            }
        }

        private void BindData()
        {
            Guid guid = Guid.Empty;
            string corpUserGuid = GetCorpUserGuid();
            if (string.IsNullOrEmpty(corpUserGuid))
            {
                return;
            }
            hdnCorpUserGuid.Value = corpUserGuid;
            hdnOperateType.Value = OperateType;
            hdnQualityGuid.Value = Guid.Empty.ToString();
            List<QueryCompanyInfoCondition> list = new List<QueryCompanyInfoCondition>();
            QueryCompanyInfoCondition condition = new QueryCompanyInfoCondition();
            condition.CorpUserGuid = new ParameterValue<Guid>() { O = OptEnum.Equal, V = Guid.Parse(corpUserGuid) };
            list.Add(condition);
            var corp = JsonConvert.DeserializeObject<CompanyInfo>(enterpriseServiceClient.GetCompanyBisicInfo(JsonConvert.SerializeObject(list), AreaCode));
            corpqual.SetRole =corp==null?"": corp.RoleId.ToString();
            ////资质存在，修改
            Guid.TryParse(QualityGuid, out guid);
            QueryCorpQualityInfoCondition queryQuality = new QueryCorpQualityInfoCondition();
            queryQuality.QualityGuid = new ParameterValue<Guid>() { V = guid, O = OptEnum.Equal };
            List<QueryCorpQualityInfoCondition> queryList = new List<QueryCorpQualityInfoCondition>();
            queryList.Add(queryQuality);
            var qualitys = JsonConvert.DeserializeObject<List<tb_Corp_Quality>>(enterpriseServiceClient.GetCompanyQualities(corpUserGuid, JsonConvert.SerializeObject(queryList), ""));
            if (qualitys == null || qualitys.Count <= 0)
            {
                return;
            }
            var qual = qualitys[0];
            chkIsMain.Checked = qual.IsMain ?? false;
            corpqual.SetQualGuid = qual.QualityGuid.ToString();

            txtQualityCode.Text = qual.QualityCode;
            txtGetDate.Value = qual.GetDate.HasValue ? ((DateTime)qual.GetDate).ToShortDateString() : "";
            txtzzzEndDate.Value = qual.zzzEndDate.HasValue ? ((DateTime)qual.zzzEndDate).ToShortDateString() : "";
            hdnIsMain.Value = (qual.IsMain ?? false).ToString();
            hdnQualityGuid.Value = qual.QualityGuid.ToString();

            Imgzz.ImageUrl = "../Handlers/GetFileHandler.ashx?fileName=" + QualityGuid + ".jpg" + "&sysCode=EF&funcCode=CorpZZ";
        }

        private void InitPage()
        {
            string UserGuid = GetCorpUserGuid();
            if (string.IsNullOrEmpty(UserGuid))
            {
                return;
            }
            Guid guid = Guid.Empty;
            Guid.TryParse(UserGuid, out guid);
            Guid corpUserGuid = guid;
            if (corpUserGuid != Guid.Empty)
            {
                OperationResult flowEditResult = new OperationResult();
                flowEditResult.Result = false;
                if (!flowEditResult.Result)
                {
                    SetControlDisable();
                }
                else
                {
                    hdnEditModel.Value = "0";
                }
            }
        }


        private Tb_Corp_CompanyInfo GetCompanyinfo(string corpUserGuid)
        {
            QueryCompanyInfoCondition queryCompanyInfoCondition =
               new QueryCompanyInfoCondition() { CorpUserGuid = new ParameterValue<Guid>() { V = Guid.Parse(corpUserGuid), O = OptEnum.Equal } };
            List<QueryCompanyInfoCondition> queryCompanyInfoList = new List<QueryCompanyInfoCondition>();
            queryCompanyInfoList.Add(queryCompanyInfoCondition);
            var corpInfo = JsonConvert.DeserializeObject<Tb_Corp_CompanyInfo>(enterpriseServiceClient.GetCompanyInfo(JsonConvert.SerializeObject(queryCompanyInfoList), ""));
            return corpInfo;
        }

        private string GetCorpUserGuid()
        {
            if (string.IsNullOrEmpty(CorpUserGuid))
            {
                return "";
            }
            return CorpUserGuid;
        }

        private void SetControlDisable()
        {
            ////不可编辑
            hdnEditModel.Value = "1";
            chkIsMain.Enabled = false;
            txtQualityCode.Enabled = false;
            txtGetDate.Disabled = true;
            txtzzzEndDate.Disabled = true;
            btnUpload.Enabled = false;
            btnSubmit.Visible = false;
            btnCancel.Visible = false;
            corpqual.Enable = false;
        }

        
    }
}